package com.emm.yixun.openapi.hander;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.app.LoginReqDto;
import com.emm.yixun.common.app.LoginResDto;
import com.emm.yixun.openapi.dto.base.BaseRequestDTO;
import com.emm.yixun.openapi.dto.base.BaseResponseDTO;
import com.emm.yixun.openapi.vo.LoginReqVo;
import com.emm.yixun.system.service.app.UserServiceAppFacade;

/**
 * 登录接口
 * @author Administrator
 *
 */
@Service("loginHander")
public class LoginHander extends com.emm.yixun.openapi.hander.base.CommonAbstractHander{
	
	@Autowired
	private UserServiceAppFacade userServiceAppFacade;
	
	@Autowired
	private CacheManager cacheManager;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		LoginReqVo req = (LoginReqVo)reqDto;
		LoginReqDto reqDto1 = req.getParams();
		BaseResponseDTO<LoginResDto> res = new BaseResponseDTO<LoginResDto>();
		res.setResData(userServiceAppFacade.login_app(reqDto1));
		//缓存userId、token、userCode
		Cache  cache = cacheManager.getCache("activeSessionsCache");
		String userId_key = res.getResData().getMerchantId()+res.getResData().getUserId()+"userId";//组装userId缓存key
		String token_key = res.getResData().getMerchantId()+res.getResData().getUserId()+"userId"+res.getResData().getToken();//组装token缓存key
		String userCode_key = res.getResData().getMerchantId()+res.getResData().getUserId()+"userCode";//组装userCode缓存key
		Element element_userId = new Element(userId_key,userId_key);
		Element element_token = new Element(token_key,token_key);
		Element elment_userCoe = new Element(userCode_key,userCode_key);
		cache.put(element_userId);
		cache.put(element_token);
		cache.put(elment_userCoe);
		return res;
	}
	
}
